<template>
  <view>
    <view class="overClass" v-if="filterLists.length" style="max-height: 100vh">
      <view v-for="item in filterLists" :key="item.id">
        <view class="wrapBox">
          <view class="smallBox">
            <text class="maxwidth"> 危废行业俗称:{{ item.wfTypeName }} </text>
          </view>
          <view class="smallBox">
            <text> 已转运:{{ item.wfTypeWeight }} </text>
            <text> 转运日期:{{ item.planTransTime }} </text>
          </view>
          <view class="smallBox">
            <text> 申请人:{{ item.userName }} </text>
            <text> 部门:{{ item.deptName }} </text>
          </view>
          <view class="flex-end">
            <button
              @click="handleLook(item, true)"
              class="commonBtnClass"
              :disabled="item.lookDisabled"
              :loading="item.lookDisabled"
              style="width: 200rpx"
            >
              查看转运单11
            </button>
            <button
              @click="handleLook(item, false)"
              class="commonBtnClass"
              :disabled="item.downDisabled"
              :loading="item.downDisabled"
              style="width: 200rpx"
            >
              下载转运单
            </button>
            <button
              @click="handleDetails(item)"
              class="commonBtnClass"
              style="width: 160rpx"
            >
              查看详情
            </button>
          </view>
        </view>
      </view>
    </view>
    <view v-else class="loadingClass"> 暂无数据 </view>
    <l-popup :radius="20" :show="popupShow">
      <view class="width60vw">
        <view style="margin-left: 20rpx; margin-top: 20rpx">{{
          titleName
        }}</view>
        <view style="margin: 20rpx">
          <view class="uni-textarea" style="max-height: 30vh; overflow-y: auto">
            <view
              style="margin-bottom: 20rpx; text-align: center"
              v-for="item in TypeListsName"
              :key="item"
              :class="{ activeClass: activeObj == item }"
              @click="handleClick(item)"
              >{{ item.wfTypeName }}</view
            >
          </view>
          <view class="classBtn">
            <button
              class="commonBtnClass width150rpx mgright20rpx"
              @click="handlebackSure(activeObj, false)"
              :disabled="activeItem?.flag"
              :loading="activeItem?.flag"
            >
              确定
            </button>
            <button class="cancelClass width150rpx" @click="popupShow = false">
              取消
            </button>
          </view>
        </view>
      </view>
    </l-popup>
  </view>
</template>

<script setup>
import lPopup from "@/uni_modules/l-popup/components/l-popup/l-popup.vue";
import { useMemberStore } from "@/stores/modules/member";
import { notice_down_pdf, show_trans_pdf } from "@/commonApi";
import { basePDFURL } from "@/utils";
const props = defineProps(["value"]);
const memberStore = useMemberStore();
const filterLists = ref([]);
const popupShow = ref(false);
const TypeListsName = ref([]);
const activeItem = ref(null);
const activeObj = ref(null);
const titleName = ref("");
let isLook = undefined;
onShow(() => {
  if (memberStore.searchLists[0]) {
    filterLists.value = memberStore.searchLists[0][props.value];
    console.log(filterLists.value, 789000);
  }
});
const handleClick = (item) => {
  activeObj.value = item;
};

const handleLook = (item, flag) => {
  titleName.value = flag ? "查看转运单" : "下载转运单";
  activeItem.value = item;
  isLook = flag;
  TypeListsName.value = [];
  const lists = item.wfTypeName ? item.wfTypeName.split(",") : [];
  TypeListsName.value = lists.map((wfTypeName) => ({
    transCode: item.transCode,
    wfTypeName,
    applyId: item.applyId,
  }));
  if (TypeListsName.value.length == 1) {
    handlebackSure(item, true);
  } else {
    if (TypeListsName.value.length > 1) {
      popupShow.value = true;
      activeObj.value = TypeListsName.value[0];
    }
  }
};
const handlebackSure = async (item, isLine) => {
  if (isLook) {
    try {
      if (isLine) {
        item.lookDisabled = true;
      } else {
        activeItem.value.flag = true;
      }
      await show_trans_pdf(item.applyId, encodeURIComponent(item.wfTypeName));
    } catch (err) {
      memberStore.srcURL = `${basePDFURL}/trans_pdf/${
        item.transCode
      }-${encodeURIComponent(item.wfTypeName)}.pdf`;
      uni.navigateTo({
        url: `/pages/PdfShow/PdfShow`,
      });
    } finally {
      if (isLine) {
        item.lookDisabled = false;
      } else {
        activeItem.value.flag = false;
      }
    }
  } else {
    try {
      if (isLine) {
        item.downDisabled = true;
      } else {
        activeItem.value.flag = true;
      }
      await notice_down_pdf(
        item.applyId,
        encodeURIComponent(item.wfTypeName),
        memberStore.userInfo.userIdDing
      );
      item.downDisabled = false;
      uni.showToast({ icon: "success", title: "下载成功" });
    } finally {
      if (isLine) {
        item.downDisabled = false;
      } else {
        activeItem.value.flag = false;
      }
    }
  }
  popupShow.value = false;
};
const handleDetails = (item) => {
  memberStore.QueryTransferDetails = item;
  uni.navigateTo({
    url: `/pages/QueryTransferDetails/QueryTransferDetails`,
  });
};
</script>

<style lang="scss" scoped>
</style>